using System;
using System.Collections.Generic;
using TagTool.Core;
using TagTool.GraphicTypes;

namespace TagTool.Nodes.Misc
{

    public class ObjectCacheSelectorTemplate<T> : BaseNode where T : new()
    {
        private InputSlot list;
        private InputSlot index;
        private OutputSlot selected;

        protected double oldIndex;
             
        public ObjectCacheSelectorTemplate()
        {
            list = new InputSlot(this, "Data in", new List<T>());
            list.ValueTypeName = "List<" + typeof(T).Name + ">";

            index = new InputSlot(this, "Index", 0.0);
            index.ParseCallback = ParseHelper.ParseDouble;
            oldIndex = 0.0;

            selected = new OutputSlot(this, "Selected", typeof(T));
            selected.ValueTypeName = typeof(T).Name;
        }

        [NodeAttribute(SlotType.Input, Show = true)]
        public InputSlot List
        {
            get { return list; }
        }

        [NodeAttribute(SlotType.Input, Show = true)]
        public InputSlot Index
        {
            get { return index; }
        }

        [NodeAttribute(SlotType.Output, Show = true)]
        public OutputSlot Selected
        {
            get { return selected; }
        }

        public override void Update(Object sender)
        {
            if (oldIndex != (double)index.Value)
            {
                oldIndex = (double)index.Value;
                if (oldIndex >= 0.0)
                {
                	if (((List<T>)list.Value).Count - 1 >= (int)oldIndex)
                	{
                    	selected.Value = ((List<T>)list.Value)[(int)oldIndex];
                	}
                }
            }
        }
        
        public override void Initialize() {
        	base.Initialize();
        	index.Value = oldIndex = -1;
        }
        
    }
}
